package com.toro.server.handler;

import com.toro.dao.GroupSession;
import com.toro.dao.GroupSessionFactory;
import com.toro.message.GroupMembersRequestMessage;
import com.toro.message.GroupMembersResponseMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ClassName: GroupMembersRequestMessageHandler
 * @Description:
 * @Author: ToroLHZ
 * @Date: 2023/12/11 17:49
 */
@Slf4j
@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, GroupMembersRequestMessage groupMembersRequestMessage) throws Exception {
        log.info("查看群成员请求：{}", groupMembersRequestMessage);
        String groupName = groupMembersRequestMessage.getGroupName();
        GroupSession groupSession = GroupSessionFactory.getGroupSession();
        List<String> members = groupSession.getMembers(groupName);
        GroupMembersResponseMessage MembersResponse = new GroupMembersResponseMessage(members, groupName);
        channelHandlerContext.writeAndFlush(MembersResponse);
        log.debug("查看群成员响应：{}", MembersResponse);
    }
}
